
import { computed, provide, onBeforeMount, ref } from "vue";
// import { useStore } from "vuex";
import { Role } from '/@/models/enum';
import { userFromStorage } from '/@/utils/userInfo'

import { ClientStorage } from '../../packages/index'


export function RoleCheck() {

    // const store = useStore();

    const userInfo = ref(ClientStorage.get('userInfo', false, true)) //不能从localStorage缓存中获取，只能是读取sessionStorage!!!
    const roleFlag = computed(() => {
        if (userInfo.value) {
            return userInfo.value.roleFlag
        } else {
            return 0
        }


    })
    const roleName = computed(() => {

        if (roleFlag.value & Role.管理员) {
            return '管理员'
        } else if ((roleFlag.value & Role.教师) == Role.教师 || (roleFlag.value & Role.课程设计师) == Role.课程设计师) {
            return '老师'
        } else
            return '其他'
    })

    const isTeacherOrAdmin = computed(() => {
        return roleName.value == '管理员' || roleName.value == '老师'
    })

    const isQuestionAdmin = computed(() => {
        if (roleFlag.value)
            return (roleFlag.value & Role.题目管理) == Role.题目管理
        else return false
    })
    const userID = computed(() => {
        if (userInfo.value)
            return userInfo.value.userID
        else
            return 0
    })
    const isAdmin = computed(() => {
        if (userInfo.value)
            return roleName.value == '管理员'
        else return false
    })
    const editCheck = (row) => {

        const result = userInfo.value.userID && row.userID == userInfo.value.userID || isAdmin.value || isQuestionAdmin.value
        return result
    }
    onBeforeMount(() => {
        userInfo.value = ClientStorage.get('userInfo', false, true) //页面加载才赋值
    })
    provide('editCheck', editCheck)
    return {
        userInfo, userID, roleName,roleFlag, isTeacherOrAdmin, isQuestionAdmin, isAdmin, editCheck
    }
}

